//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.webBasic.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webBasic.core.TocerEnvProtect;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

@TocerEnvProtect
public class BaseController {
    private static final int BUFFER_SIZE = 2048000;
    protected Log logger = LogFactory.getLog(this.getClass());
    private static final String STATUS_PARAMETER_NAME = "status";
    private static final String MESSAGE_PARAMETER_NAME = "message";
    private static final String TOKEN_PARAMETER_NAME = "token";
    private static final String JSONARRAY_PARAMETER_NAME = "data";
    private static final String VIEW_JSON = "jsonView";
    private static final String VIEW_HTML = "htmlView";
    private static final int HTTP_PORT_80 = 80;
    private static final String HTTP_IP_UNKOWN = "unknown";
    private static final String BROWSER_IE = "IE";
    private static final String BROWSER_TRIDENT = "Trident";
    private static final String BROWSER_SAFARI = "Safari";

    public BaseController() {
    }

    protected ModelAndView ajaxJSON(BaseController.Status status) {
        ModelAndView view;
        (view = new ModelAndView("jsonView")).getModel().put("status", status);
        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, JSONObject root) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("data", root);
        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, boolean needToken, HttpServletRequest request) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (needToken) {
            String tokenVal = this.uuid();
            request.getSession().setAttribute("token", tokenVal);
            data.put("token", tokenVal);
        }

        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, boolean needToken, String tokenName, HttpServletRequest request) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (needToken) {
            String tokenVal = this.uuid();
            request.getSession().setAttribute(tokenName, tokenVal);
            data.put("token", tokenVal);
        }

        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, Map<String, String> resultData) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (resultData != null) {
            Iterator status1 = resultData.entrySet().iterator();

            while(status1.hasNext()) {
                Entry<String, String> entry = (Entry)status1.next();
                data.put(entry.getKey(), entry.getValue());
            }

            resultData.clear();
        }

        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, Map<String, String> resultData, boolean needToken, HttpServletRequest request) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (resultData != null) {
            Iterator status1 = resultData.entrySet().iterator();

            while(status1.hasNext()) {
                Entry<String, String> entry = (Entry)status1.next();
                data.put(entry.getKey(), entry.getValue());
            }

            resultData.clear();
        }

        if (needToken) {
            String tokenVal = this.uuid();
            request.getSession().setAttribute("token", tokenVal);
            data.put("token", tokenVal);
        }

        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, Map<String, String> resultData, boolean needToken, String tokenName, HttpServletRequest request) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (resultData != null) {
            Iterator status1 = resultData.entrySet().iterator();

            while(status1.hasNext()) {
                Entry<String, String> entry = (Entry)status1.next();
                data.put(entry.getKey(), entry.getValue());
            }

            resultData.clear();
        }

        if (needToken) {
            String tokenVal = this.uuid();
            request.getSession().setAttribute(tokenName, tokenVal);
            data.put("token", tokenVal);
        }

        return view;
    }

    protected ModelAndView ajaxJSON(BaseController.Status status, String msg, JSONArray array) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("jsonView")).getModel()).put("status", status);
        data.put("message", msg);
        if (array != null) {
            data.put("data", array);
        }

        return view;
    }

    protected ModelAndView ajaxHTML(BaseController.Status status, String msg, Map<String, String> resultData) {
        ModelAndView view;
        Map data;
        (data = (view = new ModelAndView("htmlView")).getModel()).put("status", status);
        data.put("message", msg);
        if (resultData != null) {
            Iterator status1 = resultData.entrySet().iterator();

            while(status1.hasNext()) {
                Entry<String, String> entry = (Entry)status1.next();
                data.put(entry.getKey(), entry.getValue());
            }

            resultData.clear();
        }

        return view;
    }



    protected String getBaseUrl(HttpServletRequest request) {
        StringBuilder basePath;
        (basePath = new StringBuilder()).append(request.getScheme());
        basePath.append("://");
        basePath.append(request.getServerName());
        int port;
        if ((port = request.getServerPort()) != 80) {
            basePath.append(":").append(port);
        }

        basePath.append(request.getContextPath()).append("/");
        return basePath.toString();
    }



    public static String getPrevUrl(HttpServletRequest request) {
        StringBuffer prevUrl = request.getRequestURL();
        Map map;
        Iterator<String> iter = (map = request.getParameterMap()).keySet().iterator();

        for(int i = 0; iter.hasNext(); ++i) {
            String key = (String)iter.next();
            String[] value = (String[])map.get(key);
            if (i == 0) {
                prevUrl.append("?");
            } else {
                prevUrl.append("&");
            }

            for(int j = 0; j < value.length; ++j) {
                prevUrl.append(key).append("=").append(value[j]);
                if (j != value.length - 1) {
                    prevUrl.append("&");
                }
            }
        }

        return prevUrl.toString();
    }

    protected String getRealPath(String path, HttpServletRequest request) {
        return request.getSession().getServletContext().getRealPath(path);
    }

    protected void putValueToCookie(String cookieName, String cookieValue, Integer expiry, HttpServletResponse response) {
        Cookie cookie;
        (cookie = new Cookie(cookieName, cookieValue)).setPath("/");
        if (expiry != null) {
            cookie.setMaxAge(expiry);
        }

        response.addCookie(cookie);
    }

    protected void putValueToCookie(String cookieName, String cookieValue, String path, Integer expiry, HttpServletResponse response) {
        Cookie cookie;
        (cookie = new Cookie(cookieName, cookieValue)).setPath(path);
        if (expiry != null) {
            cookie.setMaxAge(expiry);
        }

        response.addCookie(cookie);
    }

    protected String getValueFromCookie(String cookieKeyName, HttpServletRequest request) {
        String cookieValue = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            int var4 = (cookies = cookies).length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Cookie cookie = cookies[var5];
                if (cookieKeyName.equals(cookie.getName())) {
                    cookieValue = cookie.getValue();
                    break;
                }
            }
        }

        return cookieValue;
    }

    protected String removeValueFromCookie(String removeCookieName, HttpServletRequest request, HttpServletResponse response) {
        String cookieValue = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            int var5 = (cookies = cookies).length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Cookie cookie = cookies[var6];
                if (removeCookieName.equals(cookie.getName())) {
                    cookieValue = cookie.getValue();
                    Cookie deleteCookie;
                    (deleteCookie = new Cookie(cookie.getName(), (String)null)).setPath("/");
                    deleteCookie.setMaxAge(0);
                    response.addCookie(deleteCookie);
                    break;
                }
            }
        }

        return cookieValue;
    }

    /** @deprecated */
    @Deprecated
    protected String getCookie(String name, HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies;
        if ((cookies = request.getCookies()) != null) {
            int response1 = (cookies = cookies).length;

            for(int var4 = 0; var4 < response1; ++var4) {
                Cookie cookie = cookies[var4];
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }

        return null;
    }

    /** @deprecated */
    @Deprecated
    protected void setCookie(String name, String value, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie;
        (cookie = new Cookie(name, value)).setPath(request.getContextPath() + "/");
        response.addCookie(cookie);
    }

    /** @deprecated */
    @Deprecated
    protected void setCookie(String name, String value, String path, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie;
        (cookie = new Cookie(name, value)).setPath(path);
        response.addCookie(cookie);
    }

    /** @deprecated */
    @Deprecated
    protected void setCookie(String name, String value, Integer maxAge, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = new Cookie(name, value);
        if (maxAge != null) {
            cookie.setMaxAge(maxAge);
        }

        cookie.setPath(request.getContextPath() + "/");
        response.addCookie(cookie);
    }

    /** @deprecated */
    @Deprecated
    protected void removeCookie(String name, HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie;
        (cookie = new Cookie(name, (String)null)).setMaxAge(0);
        cookie.setPath(request.getContextPath() + "/");
        response.addCookie(cookie);
    }



    protected void upload(File src, File dst) {
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(src), 2048000);
            Throwable var3 = null;
            boolean var14 = false;

            try {
                var14 = true;
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dst), 2048000);
                Throwable var4 = null;
                boolean var22 = false;

                try {
                    var22 = true;
                    byte[] buffer = new byte[2048000];

                    int length;
                    while((length = in.read(buffer)) != -1) {
                        out.write(buffer, 0, length);
                    }

                    out.flush();
                    var22 = false;
                } catch (Throwable var25) {
                    var4 = var25;
                    throw var25;
                } finally {
                    if (var22) {
                        if (var4 != null) {
                            try {
                                out.close();
                            } catch (Throwable var24) {
                                var4.addSuppressed(var24);
                            }
                        } else {
                            out.close();
                        }

                    }
                }

                out.close();
                var14 = false;
            } catch (Throwable var27) {
                var3 = var27;
                throw var27;
            } finally {
                if (var14) {
                    if (var3 != null) {
                        try {
                            in.close();
                        } catch (Throwable var23) {
                            var3.addSuppressed(var23);
                        }
                    } else {
                        in.close();
                    }

                }
            }

            in.close();
        } catch (Exception var29) {
            this.logger.error(var29.getMessage(), var29);
        }

    }

    protected void writeToClient(String fileName, String filePath, HttpServletRequest request, HttpServletResponse response) {
        File downloadFile;
        int fileLength = (int)(downloadFile = new File(filePath)).length();

        try {
            InputStream inStream = new FileInputStream(downloadFile);
            Throwable var6 = null;
            boolean var12 = false;

            try {
                var12 = true;
                String agent = request.getHeader("User-Agent");
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("download file,agent:" + agent);
                }

                if (!agent.toUpperCase().contains("IE") && !agent.contains("Trident")) {
                    fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
                } else {
                    fileName = URLEncoder.encode(fileName, "UTF-8");
                }

                String contentType = "application/x-msdownload";
                if (agent.contains("Safari")) {
                    contentType = "applicatoin/octet-stream";
                }

                contentType = contentType + ";charset=UTF-8";
                response.reset();
                response.setContentType(contentType);
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
                response.setContentLength(fileLength);
                byte[] buf = new byte[4096];
                ServletOutputStream servletOS = response.getOutputStream();

                while(true) {
                    int readLength;
                    if ((readLength = inStream.read(buf)) == -1) {
                        servletOS.flush();
                        servletOS.close();
                        var12 = false;
                        break;
                    }

                    servletOS.write(buf, 0, readLength);
                }
            } catch (Throwable var14) {
                var6 = var14;
                throw var14;
            } finally {
                if (var12) {
                    if (var6 != null) {
                        try {
                            inStream.close();
                        } catch (Throwable var13) {
                            var6.addSuppressed(var13);
                        }
                    } else {
                        inStream.close();
                    }

                }
            }

            inStream.close();
        } catch (Exception var16) {
            this.logger.error(var16.getMessage(), var16);
        }

    }

    private String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    @TocerEnvProtect
    public static enum Status {
        /** @deprecated */
        @Deprecated
        success,
        /** @deprecated */
        @Deprecated
        error,
        /** @deprecated */
        @Deprecated
        fail,
        /** @deprecated */
        @Deprecated
        warn,
        /** @deprecated */
        @Deprecated
        tokenExpire,
        /** @deprecated */
        @Deprecated
        needWechatLogin,
        /** @deprecated */
        @Deprecated
        needMemberLogin,
        /** @deprecated */
        @Deprecated
        needUserLogin,
        /** @deprecated */
        @Deprecated
        needWechatAndMemberLogin,
        /** @deprecated */
        @Deprecated
        needWechatAndUserLogin,
        SUCCESS,
        ERROR,
        FAIL,
        WARN,
        TOKEN_EXPIRE,
        NEED_WECHAT_LOGIN,
        NEED_MEMBER_LOGIN,
        NEED_USER_LOGIN,
        NEED_WECHAT_AND_MEMBER_LOGIN,
        NEED_WECHAT_AND_USER_LOGIN;

        private Status() {
        }
    }
}
